package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealOverViewVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.BufferedWriter;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class SetmealServiceImp implements SetmealService {
    @Autowired
    SetmealMapper setmealMapper;
    @Autowired
    SetmealDishMapper setmealDishMapper;
    @Autowired
    DishMapper dishMapper;
/*
* 套餐的添加
* */
    @Override
    public void save(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
//        setmeal.setCreateUser(BaseContext.getCurrentId());
//        setmeal.setUpdateUser(BaseContext.getCurrentId());
//        setmeal.setCreateTime(LocalDateTime.now());
//        setmeal.setUpdateTime(LocalDateTime.now());
        //添加一条数据
        setmealMapper.add(setmeal);
        //获取前端转过来的套餐
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if (setmealDishes !=null && setmealDishes.size()>0){
            for (SetmealDish setmealDish : setmealDishes) {
                setmealDish.setSetmealId(setmeal.getId());
            }
            setmealDishMapper.insertBatch(setmealDishes);
        }
    }
/*
* 套餐的分页查询
* */
    @Override
    public PageResult select(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
       Page<SetmealVO> page = setmealMapper.pageQuery(setmealPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    /*
    * 根据id查询套餐
    * */
    public SetmealVO getBid(Long id) {
        //根据id先查询套餐表中的数据
        Setmeal setmeal = setmealMapper.getBid(id);
        //根据套餐id查询关系表
        List<SetmealDish> setmealDishes =  setmealDishMapper.setBid(id);

        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal,setmealVO);
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }

    @Override
    public void Update(SetmealDTO setmealDTO) {
        /*
        * 根据id修改套餐
        * */
        //先删除所有的套餐里的菜品
        setmealDishMapper.DelectBid(setmealDTO.getId());
        //然后在重新添加菜品
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
            for (SetmealDish setmealDish : setmealDishes) {
                setmealDish.setSetmealId(setmealDTO.getId());
        }
        if (setmealDishes !=null && setmealDishes.size()>0){
                setmealDishMapper.insertBatch(setmealDishes);
        }
        //然后在修改套餐
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
//        setmeal.setUpdateUser(BaseContext.getCurrentId());
//        setmeal.setUpdateTime(LocalDateTime.now());
        setmealMapper.uptate(setmeal);
    }

    @Override
    public void statusupdate(Integer status, long id) {
        /*
        * 更改起售状态
        * */


        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(status);
        setmeal.setId(id);
        setmealMapper.uptate(setmeal);
    }

    @Override
    @Transactional
    public void deleteBid(List<Long> ids) {

        for (Long id : ids) {
           Setmeal setmeal =  setmealMapper.getBid(id);
            if (StatusConstant.ENABLE == setmeal.getStatus()){
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }
        for (Long id : ids) {
            //删除套餐id
            setmealMapper.deleteBid(id);
            //删除关系表中的套餐
            setmealDishMapper.DelectDishBid(id);
        }


    }
    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }

}
